/********************************
 creater:han.wen
 create time:2016-01-14
 describe：指令
 modify time:
 ********************************/
/*网络图片拦截器*/
APP.directive('countDown', ['$interval', function ($interval) {
  return {
    restrict: 'EA',
    replace: true,
    template: '<div style="text-align: center">' +
    '<span ng-bind="bindNoStart+\'：\'" ng-if="noStart">' +
    '</span>' +
    '<span ng-bind="bindText+\'：\'" ng-if="bindText&&!noStart"></span>' +
    '<span ng-bind="bindTime.day?bindTime.day+\'天\':\'0\'+\'天\'"></span>' +
    '<span ng-bind="bindTime.hour>=10?bindTime.hour+\'时\':\'0\'+bindTime.hour+\'时\'"></span>' +
    '<span ng-bind="bindTime.minute>=10?bindTime.minute+\'分\':\'0\'+bindTime.minute+\'分\'"></span>' +
    '<span ng-bind="bindTime.second>=10?bindTime.second+\'秒\':\'0\'+bindTime.second+\'秒\'"></span>' +
    '</div>',
    scope: {
      //bindMode: '@bindMode',//必填 绑定倒计时模式 0为输入倒计时天时分秒 1为输入开始时间和结束时间
      bindText: '@bindText',//必填 绑定倒计时文字
      bindNoStart: '@bindNoStart',//必填 绑定倒计时文字
      bindTime: '=bindTime',//必填 绑定倒计时时间
      bindEnd: '&bindEnd'//必填 绑定倒计时结束处理
    },
    link: function (scope, element, attrs) {
      var mode, second, minute, hour, day, startTime, endTime, timeString;
      var calculate = function (startTime, endTime) {
        var day, hour, minute, second;
        var differDay, differHour, differMinute, differSecond;
        var timeString;
        differDay = endTime.getTime() - startTime.getTime();//毫秒差
        day = Math.floor(differDay / (24 * 3600 * 1000));
        differHour = differDay % (24 * 3600 * 1000);//除去天数毫秒差
        hour = Math.floor(differHour / (3600 * 1000));
        differMinute = differHour % (3600 * 1000);//除去小时数毫秒差
        minute = Math.floor(differMinute / (60 * 1000));
        differSecond = differMinute % (60 * 1000);//除去分钟数毫秒差
        second = Math.floor(differSecond / 1000);
        timeString = day + ',' + hour + ',' + minute + ',' + second;
        return timeString;
      };
      var init = function () {
        //mode = scope.bindMode;
        scope.noStart = false;
        //if (mode == 0) {//输入时分秒
        //  second = scope.bindTime.second ? scope.bindTime.second : 0;
        //  minute = scope.bindTime.minute ? scope.bindTime.minute : 0;
        //  hour = scope.bindTime.hour ? scope.bindTime.hour : 0;
        //  day = scope.bindTime.day ? scope.bindTime.day : 0;
        //} else {//输入开始日期结束日期
        startTime = scope.bindTime.start;
        endTime = scope.bindTime.end;
        if (endTime <= startTime) {
          console.log('输入异常，结束时间应大于开始时间');
          return;
        }
        var now = new Date();
        if (now < startTime) {
          scope.noStart = true;
          timeString = calculate(now, startTime);
        } else if (now > endTime) {
          console.log('输入异常，当前时间应小于结束时间');
          return;
        } else {
          timeString = calculate(now, endTime);
        }
        day = timeString.split(',')[0];
        hour = timeString.split(',')[1];
        minute = timeString.split(',')[2];
        second = timeString.split(',')[3];
        //}
      };
      var countdown = function () {
        var interval = $interval(function () {
          if (second == 0) {
            if (minute == 0) {
              if (hour == 0) {
                if (day == 0) {
                  if (scope.noStart) {
                    init();
                    countdown();
                  } else {
                    scope.bindEnd();
                  }
                  $interval.cancel(interval);
                } else {
                  second = 59;
                  minute = 59;
                  hour = 23;
                  day--;
                }
              } else {
                second = 59;
                minute = 59;
                hour--;
              }
            } else {
              second = 59;
              minute--;
            }
          } else {
            second--;
          }
          scope.bindTime.second = second;
          scope.bindTime.minute = minute;
          scope.bindTime.hour = hour;
          scope.bindTime.day = day;
        }, 1000);
      };
      init();
      countdown();
    }
  };
}]);
